<?php 
namespace App\Ext;

class Curl{

    //curl资源句柄
    private $Handle = NULL;

    //单例模式
    static $_instance;

    //错误信息
    static $Error_Msg;

    /**
     * 初始化curl类
     * @access public
     */
    public static function New_Ch(){
            if( !(self::$_instance instanceof self) ) {    
                 self::$_instance = new self();    
            }
            self::$_instance->Handle = curl_init();
            return self::$_instance;
    }



    /**
     * 默认GET数据
     * @access public
     * @return 	$this
     */
    public function Default_Get_Set(){
	curl_setopt(self::$_instance->Handle, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt(self::$_instance->Handle, CURLOPT_HEADER, 0);
	return self::$_instance;
    }



    /**
     * 默认POST配置
     * @access 	public
     * @param 	array $Post_Date POST Data
     * @return 	$this
     */
    public function Default_Post_Set($Post_Date = array()){
	curl_setopt(self::$_instance->Handle, CURLOPT_POST, 1);
	curl_setopt(self::$_instance->Handle, CURLOPT_POSTFIELDS, $Post_Date);
	return self::$_instance;
    }



    /**
     * 设置头信息
     * @access 	public
     * @param 	array $Header_Date Set HTTP Header
     * @return 	$this
     */
    public function Header_Set($Header_Date = array()){
	curl_setopt(self::$_instance->Handle, CURLOPT_HTTPHEADER, $Header_Date);
	return self::$_instance;
    }

    /**
     * 设置等待时间
     * @access  public
     * @param   array $Header_Date Set HTTP Header
     * @return  $this
     */
    public function Time_Set($Time = NULL){
                curl_setopt(self::$_instance->Handle, CURLOPT_TIMEOUT, $Time);
                return self::$_instance;
    }



    /**
     * 获取结果
     * @access 	public
     * @param 	string $Url
     * @return 	string
     */
    public function Exec($Url = NULL){
                curl_setopt(self::$_instance->Handle, CURLOPT_URL, $Url);
                $txt = curl_exec(self::$_instance->Handle);
                if($txt === false){
                    self::$Error_Msg = curl_error(self::$_instance->Handle);
                }
                curl_close(self::$_instance->Handle);
                return $txt;

    }



    /**
     * 设置https
     * @access  public
     * @return  $this
     */
    public function Https(){
                curl_setopt(self::$_instance->Handle, CURLOPT_SSL_VERIFYPEER, FALSE);
                curl_setopt(self::$_instance->Handle, CURLOPT_SSL_VERIFYHOST, FALSE);
                return self::$_instance;
    }



    /**
     * 返回错误信息
     * @access  public
     * @return  string
     */
    public static function Error(){
                return self::$Error_Msg;
    }

}